package com.fadadianqi.mall.admin.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.mall.model.Nav;
import com.fadadianqi.mall.product.model.AttrName;
import com.fadadianqi.mall.product.model.AttrRelation;
import com.fadadianqi.mall.product.model.AttrVal;
import com.fadadianqi.mall.product.service.AttrException;
import com.fadadianqi.mall.product.service.AttrService;
import com.fadadianqi.mall.service.NavService;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by aroot on 2/12/14.
 */
@Controller
public class NavAttrController {

    private static final Log log = LogFactory.getLog(NavAttrController.class);
    @Autowired
    private AttrService attrService;
    @Autowired
    private NavService navService;

    @RequestMapping(value = "/mall/adm/nav/attr")
    public String showList(HttpServletRequest request, ModelMap modelMap) {
        String navId = ParamUtils.getString(request, "navId", null);
        List<NavAttrVO> navAttrVOList = new ArrayList<NavAttrVO>();
        List<AttrRelation> list = attrService.findNavAttrByNavId(navId);
        if (ValidatorHelper.isNotEmpty(list)) {
            for (AttrRelation attrRelation : list) {
                AttrName attrName = attrService.getAttrNameById(attrRelation.getAttrNameId());
                List<AttrVal> vals = attrService.findAttrValByAttrNameId(attrName.getId());
                NavAttrVO navAttrVO = new NavAttrVO();
                navAttrVO.setAttrRelation(attrRelation);
                navAttrVO.setAttrName(attrName);
                navAttrVO.setAttrValList(vals);
                navAttrVOList.add(navAttrVO);
            }
        }
        modelMap.put("nav", navService.getById(navId));
        modelMap.put("navAttrVOList", navAttrVOList);
        return "/mall/admin/navAttrList";
    }

    @RequestMapping(value = {"/mall/adm/nav/attr/form", "/mall/adm/nav/attr/create", "/mall/adm/nav/attr/update"})
    public String showForm(HttpServletRequest request, ModelMap modelMap) {
        String navId = ParamUtils.getString(request, "navId", null);
        Nav nav = navService.getById(navId);
        modelMap.put("nav", nav);
        List<Nav> twig = navService.showTreeTwig(nav);
        modelMap.put("twig", twig);

        String navAttrId = ParamUtils.getString(request, "navAttrId", null);
        AttrRelation attrRelation = attrService.getAttrRelationById(navAttrId);
        if (null != attrRelation) {
            NavAttrVO navAttrVO = new NavAttrVO();
            navAttrVO.setAttrRelation(attrRelation);
            AttrName attrName = attrService.getAttrNameById(attrRelation.getAttrNameId());
            navAttrVO.setAttrName(attrName);
            List<AttrVal> attrVals = attrService.findAttrValByAttrName(attrName);
            navAttrVO.setAttrValList(attrVals);
            /*modelMap.put("attrRelation", attrRelation);
            modelMap.put("attrName", attrName);
            modelMap.put("attrVals", attrVals);*/
            modelMap.put("navAttrVO", navAttrVO);
        }
        return "/mall/admin/navAttrForm";
    }

    @RequestMapping(value = "/mall/adm/ajax/nav/attr/doSave", method = RequestMethod.POST)
    public ResponseEntity<String> ajaxDoSave(HttpServletRequest request, ModelMap modelMap) {
        String navAttrId = ParamUtils.getString(request, "navAttrId", null);

        String navId = ParamUtils.getString(request, "navId", null);
        Nav nav = navService.getById(navId);
        modelMap.put("nav", nav);

        String name = ParamUtils.getString(request, "name", null);
        Boolean isRequired = ParamUtils.getBoolean(request, "isRequired", null);
        Boolean isImage = ParamUtils.getBoolean(request, "isImage", Boolean.FALSE);
        Boolean isMultiple = ParamUtils.getBoolean(request, "isMultiple", Boolean.FALSE);
        Boolean isSale = ParamUtils.getBoolean(request, "isSale", Boolean.FALSE);
        log.debug("name:" + name + ", isRequired:" + isRequired + ", isImage:" + isImage + ", isMultiple:" + isMultiple + ", isSale:" + isSale);
        String[] aliasArray = ParamUtils.getParameterValues(request, "aliasArray");
        String[] valArray = ParamUtils.getParameterValues(request, "valArray");
        String[] valIdArray = ParamUtils.getParameterValues(request, "valIdArray");
        if (log.isDebugEnabled()) {
            if (null != aliasArray) {
                log.debug("aliasArray:" + JSONProvider.toJSON(aliasArray));
            }
            if (null != valArray) {
                log.debug("valArray:" + JSONProvider.toJSON(valArray));
            }
            if (null != valIdArray) {
                log.debug("valIdArray:" + JSONProvider.toJSON(valIdArray));
            }
        }
        AttrService.AttrForm form = new AttrService.AttrForm();
        form.setName(name);
        form.setIsImage(isImage);
        form.setIsMultiple(isMultiple);
        form.setIsRequired(isRequired);
        form.setIsSale(isSale);
        form.setRelationId(navId);
        form.setType(AttrRelation.TYPE_NAV);
        form.setOrderby(0);
        if (null != valArray) {
            HashSet attrValSet = new HashSet();
            for (int i = 0; i < valArray.length; i++) {
                AttrVal attrVal = new AttrVal();
                attrVal.setVal(valArray[i]);
                attrVal.setAlias(aliasArray[i]);
                attrValSet.add(attrVal);
            }
            form.setVals(attrValSet);
        }
        String errMsg = null;
        Boolean succeed = Boolean.FALSE;
        try {
            if(ValidatorHelper.isEmpty(navAttrId)) {
                attrService.create(form);
            } else {
                form.setNavAttrId(navAttrId);
                attrService.update(form);
            }
            succeed = true;
        } catch (AttrException e) {
            e.printStackTrace();
            log.error(e.toString());
            //errMsg = messageSource.getMessage(e.getMessage(), null, Locale.getDefault());
            errMsg = e.toString();
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = new AjaxResult(form, succeed, errMsg);
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @Autowired
    private MessageSource messageSource;

    public static class NavAttrVO implements Serializable {

        private AttrRelation attrRelation;

        private AttrName attrName;

        private List<AttrVal> attrValList;

        public AttrRelation getAttrRelation() {
            return attrRelation;
        }

        public void setAttrRelation(AttrRelation attrRelation) {
            this.attrRelation = attrRelation;
        }

        public List<AttrVal> getAttrValList() {
            return attrValList;
        }

        public void setAttrValList(List<AttrVal> attrValList) {
            this.attrValList = attrValList;
        }

        public AttrName getAttrName() {
            return attrName;
        }

        public void setAttrName(AttrName attrName) {
            this.attrName = attrName;
        }


    }
}
